<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            @keyup.enter="runQuery"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="runQuery"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 数据表格区域 -->
      <el-table :data="goodList" style="width: 100%" border stripe>
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="485">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量（克）">
        </el-table-column>
        <el-table-column label="创建时间" width="110">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <!-- {{scope.row}} -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              @click="showEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="open(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <!-- 
        size-change 每页显示多少发生变化
        current-change 页码发生变化 
        current-page 当前显示的是第几页
        layout 需要展示的功能组件
      -->
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="pageNumChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑商品的对话框 -->
    <el-dialog
      title="编辑商品"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="handleEditClose"
    >
      <!-- 对话框的主要内容 -->
      <el-form
        ref="editForm"
        :model="editForm"
        label-width="80px"
        :rules="editFormRules"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品库存" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <!-- 脚部两个按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsInfo(editForm.goods_id)"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    // 获取当前页的用户数据
    this.getGoodList();
  },
  data() {
    return {
      goodList: [],
      total: 0,
      //获取用户数据列表的参数对象
      queryInfo: {
        query: "", // 查询条件
        pagenum: 1, // 页码
        pagesize: 10, // 每页条数
      },
      editDialogVisible: false, // 控制编辑用户对话框
      //   查询到的用户信息对象
      editForm: {},
      //   编辑用户信息的验证规则
      editFormRules: {
        goods_name: [
          {
            required: true,
            message: "请输入商品名称",
            trigger: "blur",
          },
        ],
        goods_price: [
          {
            required: true,
            message: "请输入商品价格",
            trigger: "blur",
          },
        ],
        goods_number: [
          {
            required: true,
            message: "请输入商品库存",
            trigger: "blur",
          },
        ],
        goods_weight: [
          {
            required: true,
            message: "请输入商品重量",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 每页条数改变了
    pageSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodList();
    },
    // 当前页码改变了 , newPageNum 就是改变后的新页码
    pageNumChange(newPageNum) {
      // 修改模型数据 queryInfo.pagenum
      this.queryInfo.pagenum = newPageNum;
      // 重新获取数据
      this.getGoodList();
    },

    async getGoodList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }

      this.goodList = res.data.goods;
      this.total = res.data.total;

      // console.log(this.total);
      // console.log(res.data);
    },

    // 执行搜索功能
    runQuery() {
      this.getGoodList();
    },

    // 编辑用户对话框关闭前
    handleEditClose() {
      // 将编辑用户的表单清除
      // this.$refs.editForm.resetFields();
      this.editDialogVisible = false;
    },
    // 展示用户
    async showEdit(goods) {
      this.editDialogVisible = true;
      // console.log(user);
      const { data: res } = await this.$http.get(`goods/${goods.goods_id}`);
      this.editForm = res.data;
    },
    // 编辑确认
    editGoodsInfo(id) {
      this.$refs.editForm.validate(async (result) => {
        console.log(result);
        if (result) {
          const { data: res } = await this.$http.put(
            `goods/${id}`,
            this.editForm
          );
          if (res.meta.status !== 200) {
            console.log(res.meta);
            return this.$message.error(res.meta.msg);
          }
          this.$message.success(res.meta.msg);
          // this.$refs.editForm.resetFields();
          this.editDialogVisible = false;

          this.getGoodList();
        }
      });
    },

    // 打开确认删除框
    open(id) {
      // console.log(id);
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`users/${id}`);
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg);
          } else {
            this.$message.success(res.meta.msg);
            this.getUserList();
            this.$router.go(0);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "那就不删了奥",
          });
        });
    },

    // 添加商品
    addGoods() {
      this.$router.push("addGoods");
    },
  },
};
</script>

<style scoped>
.el-card {
  margin-top: 15px;
}
.el-table {
  margin: 15px 0;
}
</style>
